package com.putong35.common.baseactfrag.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.LoadState;
import com.putong35.common.baseactfrag.OnListDataUpdateListener;

import java.util.List;

/**
 * Activity定义了许多的状态，用户可以通过set(status)来切换状态，每个状态对应一个View，用户还可以自定义这个View
 * 
 * 每个状态对应一个UI 布局， 用户可以自定义这些布局通过setXX方法（e.g setEmptyView）
 * 
 * @author Vivi Peng
 * 
 */
public abstract class BaseListAcivity extends RecyclerViewBaseActivity implements  OnListDataUpdateListener {


	@Override
	public RecyclerView.LayoutManager createLayoutManager() {
		LinearLayoutManager layoutManager = new LinearLayoutManager(this);
		layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		return layoutManager;
	}


	@Override
	public void onListDataUpdateSuccess(List list) {
		if (list != null && list.size() > 0) {
			getAdapter().getData().addAll(list);
			getAdapter().notifyItemInserted(getAdapter().getData().size());
		}
		// state maybe refreshing, need to change state here
		if (getAdapter().getData().size() == 0) {
			setState(CompState.EMPTY);
		} else {
			setState(CompState.DATA);
		}
		// TODO 状态要细化
		mCompHelper.setLoadState(LoadState.IDLE);
	}

	@Override
	public void onListDataUpdateFailed(int code, String msg) {
		setState(CompState.EMPTY_ERROR);
	}

}
